<?php
namespace Home\Controller;
use Think\Controller;
class ModuleController extends Controller {
    public function tags(){
     $Newsmodel=M('content'); 
     $count=$Newsmodel  
             -> field('category')
             ->group('category')
             ->select();
      $num=array();   
    foreach($count as $key=>$value){    
    $num[$value[category]]=$Newsmodel->where($count[$key])->count();     
   }   
        $this->assign('num',$num) ;
        $this->assign('data',$data);               
        $this->display();
    }
     public function users(){
         $Newsmodel=M('user');
         $data=$Newsmodel->where('contribution>0')->order('contribution desc')->limit(7)->select();      
        $this->assign('data',$data);
        $this->display();
    }
    public function guide(){
        $this->display();
    }
    public function feedback(){
        $this->display();
    }
    public function addmodule(){
         require_once LIB_PATH. 'custom/securimage/securimage.php';
        $securimage = new\ Securimage();
        $captcha=I('post.captcha');   
       if ($securimage->check($captcha) == false){
        $this->error('验证码错误','feedback');
       }else{    
        $modelModule=D('module');             
        $modelModule->register($_POST['module'],$_POST['version']);    
        $this->success('提交成功','feedback');
    }
    }
    public function add(){
        $this->display();
    }
    public function doadd(){          
        if(session('isLogin')==ture){            
        require_once LIB_PATH. 'custom/securimage/securimage.php';
        $securimage = new\ Securimage();
        $captcha=I('post.captcha');   
       if ($securimage->check($captcha) == false){
            $output=array();
            $output['status']=1;
            $output['info']="验证码错误";
            $output['url']=U('add');
            print json_encode($output);
            return false;
        $this->error('验证码错误','add');
       }else{
        $info=  pathinfo($_FILES['img']['name']);          
        if($_FILES['img']['error']==0){
            move_uploaded_file($_FILES['img']['tmp_name'], THINK_PATH ."/../Public/upload/".md5(time()).".".$info['extension']) ; 
            $data['img']= 'THINK_PATH '.'/../Public/upload/'.md5(time()).'.'.$info['extension'];      
        }     
        $Newsmodel=M('content');
        $News=M('user');
        $data['title']=$_POST['title'];
        $data['version']=$_POST['version'];
        $data['category']=$_POST['category'];
        $data['body']=$_POST['body'];      
        $map=$News->where('id='.session('id'))->find();
        $map['contribution']=$map['contribution']+1;        
        $News->where('id='.session('id'))->save($map);      
        $Newsmodel->data($data)->add();
        $this->redirect('add');
    }
    }
    else{
        $output=array();
        $output['status']=0;
        $output['info']="请先登录";
        $output['url']=U('add');
        print json_encode($output);
        return false; 
        $this->error('请登录','add');
    }    
    }
    public function read(){
        $Newsmodel=M('user');
        $map['id']=$_GET['id'];
        $data=$Newsmodel->where($map)->find();
        $this->assign('data',$data);
        $this->display();       
    }
    public function lists(){
        $Newsmodel=M('content');
        $map['category']=$_GET['category'];
        $data=$Newsmodel->where($map)->select();   
        $this->assign('data',$data);
        $this->display();
    }
    public function update(){
         $Newsmodel=M('user');
         $map['id']=session('id');
         $data=$Newsmodel->where($map)->find();
         $this->assign('data',$data);
        $this->display();
    }
    public function doupdate(){
        $Newsmodel=M('user');
        $map['id']=$_POST['id'];
        $data['name']=$_POST['name'];
        $data['pass']=$_POST['pass'];
        if($_FILES['photo']['error']==0){
            move_uploaded_file($_FILES['photo']['tmp_name'], THINK_PATH ."/../Public/upload/".md5(time()).".png") ; 
            $data['photo']= 'THINK_PATH '.'/../Public/upload/'.md5(time()).'.png';
       
        }
        $Newsmodel->where($map)->save($data);
        $this->success('上传成功','/../Thinkphp/Home/user/login');
    }
            
}